<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>网站设置</span>
      </div>
      <div>
        <div class="main_box">
          <el-form :model="webinfo" ref="ruleForm" label-position="left" label-width="180px">
            <el-form-item label="网站logo:">
              <button class="logoup_btn" @click="logoDialogVisible = true">
                <el-image class="logoimg" :src='weblogo'>
                  <div slot="error" class="image-slot error_css">
                    <i class="el-icon-picture-outline"></i>
                  </div>
                </el-image>
              </button>
            </el-form-item>
            <el-form-item label="是否开启评论：">
              <el-switch v-model="webinfo.comment" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
            <el-form-item label="是否开启评论审核：">
              <el-switch v-model="webinfo.review" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
            <el-form-item class="subbtn_box">
              <el-button type="primary" class="subbtn" @click="setweb">提交</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>

    <!-- 网站logo的dialog -->
    <el-dialog title="提示" :visible.sync="logoDialogVisible" width="60%" @close="addweblogoDialog">
      <div class="logo_cut">
        <div class="cropper-content">
          <div class="cropper">
            <vueCropper ref="cropper" :img="option.img" :outputSize="option.size" :outputType="option.outputType" :info="true" :full="option.full" :canMove="option.canMove" :canMoveBox="option.canMoveBox" :original="option.original" :autoCrop="option.autoCrop" :autoCropWidth="option.autoCropWidth" :autoCropHeight="option.autoCropHeight" :fixedBox="option.fixedBox" @realTime="realTime">
            </vueCropper>
          </div>
          <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden', 'margin': '5px'}">
            <div :style="previews.div" class="preview">
              <img :src="previews.url" :style="previews.img">
            </div>
          </div>
        </div>

        <div class="footer-btn">
          <div class="scope-btn">
            <label class="btn" for="uploads">更换图片</label>
            <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/jpg" @change="uploadImg($event, 1)">

            <button @click="changeScale(1)"><i class="el-icon-zoom-in"></i></button>
            <button @click="changeScale(-1)"><i class="el-icon-zoom-out"></i></button>
            <button @click="rotateLeft"><i class="el-icon-refresh-right"></i></button>
            <button @click="rotateRight"><i class="el-icon-refresh-left"></i></button>
          </div>
          <div class="upload-btn">
            <button type="primary" @click="uploadavatar">上传<i class="el-icon-upload el-icon--right"></i></button>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="logoDialogVisible = false">取 消</el-button>
        <el-button @click="logoDialogVisible= false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      crap: false,
      previews: {},
      option: {
        img: this.InitializeAvatar,
        size: 1,
        full: false, // 输出原图比例截图
        outputType: 'png',
        canMove: true,
        original: false,
        canMoveBox: true,
        autoCrop: true,
        autoCropWidth: 160,
        autoCropHeight: 80,
        fixedBox: true
      },
      webinfo: {},
      setwebinfo: {
        // 标题
        title: '资源共享',
        // logo图片
        logo: null,
        // 是否开启评论
        comment: true,
        // 是否开启评论审核
        review: true
      },
      // 网站logo的dialog
      logoDialogVisible: false,
      // 网站logo的beas64
      weblogo: ''
    }
  },
  created() {
    this.getwebinfo()
  },
  methods: {
    // 获取网站设置信息
    async getwebinfo() {
      const res = await this.$http.get('/settings')
      // console.log(res)
      this.webinfo = res.data
      this.weblogo = res.data.logo
    },
    // logo裁剪区
    // 修改图片大小
    changeScale(num) {
      num = num || 1
      this.$refs.cropper.changeScale(num)
    },
    // 向左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft()
    },
    // 向右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight()
    },
    // 实时预览函数
    realTime(data) {
      this.previews = data
    },
    // 上传头像
    uploadavatar() {
      this.$refs.cropper.getCropData(async (data) => {
        // console.log(data)
        const that = this
        this.weblogo = data
        this.webinfo.logo = this.weblogo
        that.$message({
          showClose: true,
          message: 'logo标志上传成功！',
          type: 'success'
        })
      })
    },
    uploadImg(e, num) {
      // 上传图片
      // this.option.img
      const that = this
      var file = e.target.files[0]
      if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
        that.$message({
          showClose: true,
          message: '图片类型必须是.gif,jpeg,jpg,png,bmp中的一种!',
          type: 'error'
        })
        return false
      }
      var reader = new FileReader()
      reader.onload = (e) => {
        let data
        if (typeof e.target.result === 'object') {
          // 把Array Buffer转化为blob 如果是base64不需要
          data = window.URL.createObjectURL(new Blob([e.target.result]))
        } else {
          data = e.target.result
        }
        if (num === 1) {
          this.option.img = data
        } else if (num === 2) {
          this.example2.img = data
        }
      }
      // 转化为base64
      reader.readAsDataURL(file)
      // 转化为blob
      // reader.readAsArrayBuffer(file)
    },
    addweblogoDialog() {
      this.weblogo = this.webinfo.logo
    },
    async setweb() {
      this.setwebinfo.title = this.webinfo.title
      this.setwebinfo.logo = this.webinfo.logo
      this.setwebinfo.comment = this.webinfo.comment
      this.setwebinfo.review = this.webinfo.review
      const formdata = this.$qs.stringify(this.setwebinfo)
      // console.log(this.setwebinfo)
      // console.log(formdata)
      const res = await this.$http({
        method: 'post',
        url: '/settings',
        data: formdata
      })
      // console.log(res)
      if (res.status !== 200) {
        return this.$message({
          showClose: true,
          message: '网站信息设置失败!',
          type: 'error'
        })
      }
      this.$message({
        showClose: true,
        message: '网站信息设置成功!',
        type: 'success'
      })
    }
  }
}
</script>

<style lang='less' scoped>
.main_box {
  width: 500px;
  padding-left: 40px;
  margin: 30px auto;
}

.logoup_btn {
  padding: 0;
  width: 140px;
  height: 70px;
  border: none;
  border-radius: 4px;
}

.logoup_btn .logoimg,
.logoup_btn .logoimg .error_css {
  width: 100%;
  height: 100%;
  border-radius: 4px;
  line-height: 70px;
  font-size: 20px;
}

.logo_cut {
  padding-left: 60px;
}

.cropper-content{
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
  .cropper{
    width: 350px;
    height: 300px;
  }
  .show-preview{
    flex: 1;
    width: 160px;
    height: 80px;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    .preview{
      overflow: hidden;
      border:1px solid #cccccc;
      background: #cccccc;
      margin-left: 40px;
    }
  }
}
.show-preview .preview {
  width: 160px !important;
  height: 80px !important;
  margin-left: 0 !important;
}
.footer-btn{
  margin-top: 30px;
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
  .scope-btn{
    width: 350px;
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    -webkit-justify-content: space-between;
  }
  .upload-btn{
    flex: 1;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    button {
      width: 80px;
      color: #fff;
      font-size: 14px;
      background-color: #67c23a;
    }
  }
  .btn {
    outline: none;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    -webkit-appearance: none;
    text-align: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    outline: 0;
    margin: 0;
    -webkit-transition: .1s;
    transition: .1s;
    font-weight: 500;
    padding: 8px 15px;
    font-size: 12px;
    border-radius: 3px;
    color: #fff;
    font-size: 14px;
    background-color: #409EFF;
    border-color: #409EFF;
  }
  button {
    border: none;
    border-radius: 3px;
  }
}

.subbtn_box{
  margin-right: 180px;
  text-align: center;
}
.subbtn {
  width: 160px;
}
</style>
